package com.higgins.lawapp.client.maintenance.handlers;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.TextBox;
import com.higgins.lawapp.client.maintenance.components.DataItemTable;
import com.higgins.lawapp.client.maintenance.model.DataItemModel;
import com.higgins.lawapp.client.maintenance.presenter.DataItemPresenter;

public class DataItemTableHandler implements ClickHandler	{
	private DataItemPresenter dataItemPresenter;
	private DataItemModel dataItemModel;
	private DataItemTable dataItemTable;
	private final TextBox textBox = new TextBox();
	private int minRowEditable=1;
	private int row;
	private int column;
	private int rowHistory=0;
	private int columnHistory=0;
	private boolean editing=false;
	
	
	public DataItemTableHandler(DataItemPresenter dataItemPresenter, DataItemModel dataItemModel, DataItemTable dataItemTable)	{
		this.dataItemPresenter = dataItemPresenter;
		this.dataItemModel = dataItemModel;
		this.dataItemTable = dataItemTable;
		dataItemTable.addDataItemTableHandler(this);
		textBox.addKeyDownHandler(new KeyDownHandler()	{
			public void onKeyDown(KeyDownEvent event)	{
				int code = event.getNativeKeyCode();
				if (KeyCodes.KEY_ENTER == code)	{
					stopEditingCell(textBox, row, column);
				}
			}
		});		
	}

	@Override
	public void onClick(ClickEvent event) {
		Cell cell = dataItemTable.getCellForEvent(event);
		row = cell.getRowIndex();
		column = cell.getCellIndex();
		cellClicked(row, column);
		if ((editing) && !(row==rowHistory && column==columnHistory))		{
			stopEditingCell(textBox, rowHistory, columnHistory);		
		}
		if (row>=minRowEditable && !editing && columnEditable(column))	{
			editing = true;
			String text = dataItemTable.getText(row, column);
			int rowHeight = dataItemTable.getRowFormatter().getElement(row).getClientHeight();
			textBox.setHeight("" + rowHeight + "px");
			dataItemTable.setWidget(row, column, textBox);
			textBox.setText(text);
			textBox.setFocus(true);		
			rowHistory=row;
			columnHistory=column;
		}
	}
	private void cellClicked(int row, int column)	{
		dataItemModel.setSelectedRow(row);
		switch(column)	{
		case 0:
			if (row == 0)
				selectAllCheckBoxes();
			break;
		case 3:
			if (row == 0)
				dataItemModel.deleteRows();
			else
				dataItemModel.deleteRow();	
			break;
		}
	}
	
	private boolean columnEditable(int column)	{
		switch(column)	{
		case 1:
			return true;	
		case 2:
			return true;	}
		return false;
	}
	
	private void stopEditingCell(TextBox textBox, int row, int column)	{
		if (editing==true)	{
			String text = textBox.getText();
			dataItemTable.remove(textBox);
			dataItemTable.setText(row, column, text);
			dataItemModel.updateDataItemDTO(text, row, column);
			editing=false;
		}
	}
	private void selectAllCheckBoxes()	{
		int rowCount = dataItemTable.getRowCount();
		int row=0;
		CheckBox topCheckBox = (CheckBox)dataItemTable.getWidget(row, 0);
		boolean topCheckBoxValue = topCheckBox.getValue();
		row++;
		while (row < rowCount)	{
			CheckBox checkBox = (CheckBox)dataItemTable.getWidget(row, 0);
			checkBox.setValue(topCheckBoxValue);
			row++;
		}
	}
}
